﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace odinshrine
{
    class Commandes:Menu
    {
        static TimeSpan timer = TimeSpan.Zero;
        public static int position = 0;
        static SpriteFont font;
        static Texture2D retour, Ecrancommandes;
        public static int retourX = 250, retourY = 500;
        static string[] boutons = new string[6];
        public static Keys[] commandes = new Keys[6];
        public static bool ecrire = false;

        public static void LoadCommandes(ContentManager Content)
        {
            Ecrancommandes = Content.Load<Texture2D>("Menu/ecrancommandes");
            retour = Content.Load<Texture2D>("Menu/retour");

            font = Content.Load<SpriteFont>("Fonts/menufont");
            boutons[0] = "gauche";
            boutons[1] = "droite";
            boutons[2] = "tir";
            boutons[3] = "haut";
            boutons[4] = "bas";
            boutons[5] = "saut";
            try
            {
                Deserialize("commandes.txt");
            }
            catch
            {
                commandes[0] = Keys.A;
                commandes[1] = Keys.D;
                commandes[2] = Keys.N;
                commandes[3] = Keys.W;
                commandes[4] = Keys.S;
                commandes[5] = Keys.Space;
            }
        }

        public static void Gestion(GameTime gametime, KeyboardState clavier)
        {
            if (menu_commandes)
            {
                clavier = Keyboard.GetState();

                if (position == boutons.GetLength(0) + 1 && clavier.IsKeyDown(Keys.Enter))
                {
                    menu_commandes = false;
                    Options.commandesRect.X = Options.commandesX;
                    Options.commandesRect.Y = Options.commandesY;
                    Options.menuRect.X = Options.menuX;
                    Options.menuRect.Y = Options.menuY;
                }
                
                if (timer.Milliseconds >= 180 && !ecrire)
                {
                    clavier = Keyboard.GetState();
                    if (clavier.IsKeyDown(Keys.Down) && position != boutons.GetLength(0) + 1)
                    {
                        position++;
                    }
                    else if (clavier.IsKeyDown(Keys.Up) && position != 1)
                    {
                        position--;
                    }
                    timer = TimeSpan.Zero;
                }

                for (int i = 0; i < boutons.GetLength(0); i++)
                {
                    if (i + 1 == position)
                    {
                        if (clavier.IsKeyDown(Keys.Enter) && timer.Milliseconds >= 150)
                        {
                            ecrire = !ecrire;
                            timer = TimeSpan.Zero;
                        }
                    }
                }

                if (ecrire)
                {
                    try
                    {
                        if (!clavier.IsKeyDown(Keys.Enter))
                        {
                            commandes[position - 1] = clavier.GetPressedKeys()[0];
                        }
                    }
                    catch { }
                }
                else
                {
                    Serialize(commandes, "commandes.txt");
                }

                timer += gametime.ElapsedGameTime;
            }
        }

        private static void Serialize(Keys[] tabKeys, string filename)
        {
            try
            {
                Stream st = File.OpenWrite(filename);
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(st, tabKeys);
                st.Close();
            }
            catch { }
        }

        private static void Deserialize(string filename)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            object obj = formatter.Deserialize(File.Open(filename, FileMode.Open));
            Keys[] savedcommandes = (Keys[])obj;
            commandes[0] = savedcommandes[0];
            commandes[1] = savedcommandes[1];
            commandes[2] = savedcommandes[2];
            commandes[3] = savedcommandes[3];
            commandes[4] = savedcommandes[4];
            commandes[5] = savedcommandes[5];
        }

        public static void Draw(SpriteBatch spritebatch)
        {
            spritebatch.Draw(Ecrancommandes, new Vector2(0, 0), Color.White);
            spritebatch.Draw(retour, new Vector2(retourX, retourY), Color.White);

            int accuY = 50;
            for (int i = 0; i < boutons.GetLength(0); i++)
            {
                try
                {
                    if (i + 1 == position && !ecrire)
                    {
                        spritebatch.DrawString(font, boutons[i], new Vector2(20, accuY), Color.Red);
                        spritebatch.DrawString(font, commandes[i].ToString(), new Vector2(120, accuY), Color.Red);
                    }
                    else if (i + 1 == position && ecrire)
                    {
                        spritebatch.DrawString(font, boutons[i], new Vector2(20, accuY), Color.Green);
                        spritebatch.DrawString(font, commandes[i].ToString(), new Vector2(120, accuY), Color.Green);
                    }
                    else
                    {
                        spritebatch.DrawString(font, boutons[i], new Vector2(20, accuY), Color.White);
                        spritebatch.DrawString(font, commandes[i].ToString(), new Vector2(120, accuY), Color.White);
                    }
                    accuY += 50;
                }
                catch { }
            }
            if (position == boutons.GetLength(0) + 1)
            {
                spritebatch.Draw(Mainmenu.imgBouton, new Vector2(retourX - 70, retourY), Color.White);
            }

            spritebatch.DrawString(font, ecrire.ToString(), new Vector2(500, 100), Color.White);
        }
    }
}
